﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using SOM.AbstractDALFactory;
using SOM.Common;
using SOM.ConcreteBL;
using SOM.ConcreteDAL;
using SOM.ConcreteDALFactory;
using SOM.Model;
using SOM.Facade;

public partial class Reclamacao_ReclamacoesUsuario : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Usuario usuario = Common.RetornarUsuarioSessao(this);
            ReclamacaoBL reclamacaoBL = new ReclamacaoBL();

            if (usuario == null)
            {
                FormsAuthentication.RedirectToLoginPage();
                return;
            }
            
            CarregarGrid();
        }

        if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"].ToString() != string.Empty)
        {
            ExcluirReclamacao(int.Parse(Request["__EVENTARGUMENT"]));
        }

    }
    protected void btnPesquisar_Click(object sender, EventArgs e)
    {
        CarregarGrid();
    }

    //Atualiza o gridview de reclamacoes
    private void CarregarGrid()
    {
        Usuario usuario = Common.RetornarUsuarioSessao(this);
        ReclamacaoBL reclamacaoBL = new ReclamacaoBL();

        if (usuario == null)
        {
            FormsAuthentication.RedirectToLoginPage();
            return;
        }

        //Se for vazio retorna todas as reclamacoes do usuario
        if (string.IsNullOrEmpty(txtPesquisa.Text.Trim()))
        {
            grvReclamacoesUsuario.DataSource = reclamacaoBL.FindByUsuario(usuario.usr_ds_nickname);
            grvReclamacoesUsuario.DataBind();
            return;
        }

        //Metodo de pesquisa por descricao
        if (rblMetodoPesquisa.SelectedItem.Value.Equals("descricao"))
        {
            grvReclamacoesUsuario.DataSource =
                reclamacaoBL.FindByUsuarioDescricao(txtPesquisa.Text.Trim(), usuario.usr_ds_nickname);
            grvReclamacoesUsuario.DataBind();
            return;
        }

        //Metodo de pesquisa por palavra-chave
        if (rblMetodoPesquisa.SelectedItem.Value.Equals("palavraChave"))
        {
            grvReclamacoesUsuario.DataSource = reclamacaoBL.FindByUsuarioPalavraChave(txtPesquisa.Text.Trim(),
                                                                     usuario.usr_ds_nickname);
            grvReclamacoesUsuario.DataBind();

        }
    }

    private void ExcluirReclamacao(int idReclamacao)
    {
        try
        {
            //SOMAbstractDALFactory factory = new SOMConcreteDALFactory();
            //ReclamacaoDAL reclamacaoDAL = factory.CreateReclamacaoDAL();
            ReclamacaoBL reclamacaoBL = new ReclamacaoBL();

            reclamacaoBL.Delete(idReclamacao);

            CarregarGrid();
        }
        catch (Exception ex)
        {
            lblMensagem.Text = ex.Message + ":" + ex.InnerException;
        }
    }
    protected void grvReclamacoesUsuario_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HyperLink hlkExcluirReclamacao = (HyperLink)e.Row.FindControl("hlkExcluirReclamacao");
            hlkExcluirReclamacao.NavigateUrl = "javascript:Excluir('" + e.Row.Cells[0].Text + "')";

            HyperLink hplVerMapa = (HyperLink)e.Row.FindControl("hplVerMapa");
            hplVerMapa.NavigateUrl =
                "javascript:AbrirPopup(\"../Reclamacao/PopupReclamacaoLocalizacao.aspx?op=1&recId=" +
                SOM.Security.SOMSecurity.Codificar(e.Row.Cells[0].Text) + "\")";

            HyperLink hplReclamacaoDetalhes = (HyperLink)e.Row.FindControl("hypVisualizarReclamacao");
            string idReclamacao = SOM.Security.SOMSecurity.Codificar(e.Row.Cells[0].Text);
            hplReclamacaoDetalhes.NavigateUrl = "~/Reclamacao/ReclamacaoDetalhes.aspx?op=1&recId=" + idReclamacao;

            //Label lblStatusReclamacao = (Label)e.Row.FindControl("lblStatusReclamacao");
            //lblStatusReclamacao.Text = ((StatusDaReclamacao)int.Parse(lblStatusReclamacao.Text)).ToString();
        }
    }
}
